﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Model;
using UCNKompetenceBors.Database;

namespace UCNKompetenceBors.Exchange
{
    public partial class purchaseCreate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ckeditorCreatePurchaseDescription.Width = 600;
                lblCreatePurchaseSuccess.Visible = false;
                this.Form.DefaultButton = btnCreatePurchase.UniqueID;

                PopulateCategoryDropDown();

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();
                if(!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Populates the category dropdown list.
        /// </summary>
        private void PopulateCategoryDropDown()
        {
            List<Category> categories = dbExchange.GetAllCategories();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (categories.Count > 0)
            {
                ddlCreatePurchaseCategory.DataSource = categories;
                if (!culture.Equals("en-GB"))
                    ddlCreatePurchaseCategory.DataTextField = "CategoryName";
                else
                    ddlCreatePurchaseCategory.DataTextField = "LocalizedCategoryName";

                ddlCreatePurchaseCategory.DataValueField = "CategoryID";
                ddlCreatePurchaseCategory.DataBind();

                ddlCreatePurchaseCategory.Items.Insert(0, new ListItem("Vælg Kategori"));
            }
        }

        /// <summary>
        /// Creates a purchase.
        /// </summary>
        private void CreatePurchase()
        {
            string title = txtCreatePurchaseTitle.Text;
            string desc = ckeditorCreatePurchaseDescription.Text;
            int categoryId = Convert.ToInt32(ddlCreatePurchaseCategory.SelectedValue);

            dbExchange.CreatePurchase(title, desc, categoryId);
        }

        protected void btnCreatePurchase_Click(object sender, EventArgs e)
        {
            CreatePurchase();
            lblCreatePurchaseSuccess.Visible = true;
            panCreatePurchaseForm.Visible = false;
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnCreatePurchase.Text = GetLocalResourceObject("btnCreatePurchaseText").ToString();
            lblCreatePurchaseCategory.Text = GetLocalResourceObject("lblCreatePurchaseCategoryText").ToString();
            lblCreatePurchaseDescription.Text = GetLocalResourceObject("lblCreatePurchaseDescriptionText").ToString();
            lblCreatePurchaseHeader.Text = GetLocalResourceObject("lblCreatePurchaseHeaderText").ToString();
            lblCreatePurchaseSuccess.Text = GetLocalResourceObject("lblCreatePurchaseSuccessText").ToString();
            lblCreatePurchaseTitle.Text = GetLocalResourceObject("lblCreatePurchaseTitleText").ToString();
            
            string categoryValidation = GetLocalResourceObject("rfvalCreatePurchaseCategoryText").ToString();
            rfvalCreatePurchaseCategory.ToolTip = categoryValidation;
            rfvalCreatePurchaseCategory.ErrorMessage = categoryValidation;
            
            string descriptionValidation = GetLocalResourceObject("rfvalCreatePurchaseDescriptionText").ToString();
            rfvalCreatePurchaseDescription.ToolTip = descriptionValidation;
            rfvalCreatePurchaseDescription.ErrorMessage = descriptionValidation;
            
            string titleValidation = GetLocalResourceObject("rfvalCreatePurchaseTitleText").ToString();
            rfvalCreatePurchaseTitle.ToolTip = titleValidation;
            rfvalCreatePurchaseTitle.ErrorMessage = titleValidation;
            valsumCreatePurchase.HeaderText = GetLocalResourceObject("valsumCreatePurchaseText").ToString();

            string categoryDummy = GetLocalResourceObject("ddlCreatePurchaseCategoryDummyObjectText").ToString();
            ddlCreatePurchaseCategory.Items[0].Text = categoryDummy;
            rfvalCreatePurchaseCategory.InitialValue = categoryDummy;

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (culture.Equals("en-GB"))
                ckeditorCreatePurchaseDescription.Language = "en";
            else
                ckeditorCreatePurchaseDescription.Language = "da";
        }
    }
}